\section{Specification of Single-Queue Simulator Design (Scenario I)}

This section is discussing about the basic of the simulator, and it's the \emph{core} of the hole simulator because of \emph{Random Numbers Generator}, events managing and so on.

\subsection{Simulator times \& Random Numbers Generator}
In the assignment's condition, the argument of the simulator is:
\begin{description}
  \item[Offered Load] $a = 0.8 Erlangs$
  \item[Mean Packet length] $length=8000 bits$
  \item[Maximum Queue Length] $K=10$
  \item[Link rate] $linkrate=100 Gbps$
  \item[Queue discipline] FIFO
\end{description}

With the server number
\begin{equation}
m=1
\end{equation}
, we can get these conclusions:
\begin{equation}
a
\triangleq{}\frac{\lambda{}}{\mu{}}
=\frac{\lambda{}}{\mu{}}
=0.8
\end{equation}
\begin{equation}
\mu
=\frac{linkrate}{length}
=\frac{10^{11}}{8000}
=1.25\times{}10^7 p/s
\end{equation}
\begin{equation}
\lambda
=a\mu
=0.8\times{}10^7
=1\times{}10^7 p/s
\end{equation}

So, the mean service time:
\begin{equation}
  \mst = \frac{1}{\mu} = 8 \times{} 10^{-8} s/bits
\end{equation}

and the mean inter-arrival time:
\begin{equation}
  \iat = \frac{1}{\lambda} = 1 \times{} 10^{-7} s/bits
\end{equation}

How to generator the numbers in \ed{}?
The \cdf{} of \ed{} is:
\begin{equation*}
  F(x;\lambda{}) = \left\{
 \begin{array}{rl}
  1-e^{-\lambda{}x} & , x \geq{} 0\\
   0 & , x < 0
 \end{array} \right.
\end{equation*}

Let $F(x;\lambda{})=y$, get the $f(y)=x$:
\begin{align}
  y &=1-e^{-\lambda{}x}\\
  e^{-\lambda{}x} &= 1-y\\
  -\lambda{}x &= \ln{(1-y)}\\
  x &=-\frac{1}{\lambda}\ln{(1-y)} \label{eq:exp}
\end{align}

With the equation~\eqref{eq:exp}, we can get the random number by \emph{evenly distributed}:
\begin{align}
  \mu{} &= \frac{1}{\lambda}\\
  x &= -\mu{}\ln{(1-y)}
\end{align}

So the generator can be:
\lstinputlisting[label=src:exprand,caption=Generate exponential distributed by \erlang{} language API, linerange={86-91}]{../../utils_src/edm_utils.erl}

\subsection{Departure Time Calculation}
Because of depending on the \emph{packet's arriving time}, \emph{bits length} and \emph{the front packet's completed time}, the calculation of the packet's departure time depends not only the self information but also the simulator's environment.

In the network, each router should save the last departure packet's completing time and the link rate. And the calculation is Algorithm~\ref{alg:departure}.%, and the implementation of the Algorithm is~\ref{src:departure}.

\NumberProgramstrue
\begin{algorithm}
\bfvariables
\begin{program}
lastCTime \gets{} 0 \rrcomment{Support the last departure packet's completing time}
rate \gets 10^{11} \rrcomment{Link Rate}
\DO
  packet \gets{} |next arrive packet|
  \IF |size|(buffer)<10 \rrcomment{packet can be inserted into buffer}
  \THEN
    packet[|departure|] \gets{} \frac{packet[|length|]}{rate}+|max|(packet[|arrive|], lastCTime)
    lastCTime \gets{} packet[|departure|]
  \FI
\OD
\end{program}
\label{alg:departure}
\end{algorithm}

%\lstinputlisting[label=src:departure,caption=Packet Arriving processing in \erlang{} language, linerange={10-47}]{../../event_src/router_manager.erl}


\subsection{Event processing loop}
At begin, there are two types of event: \emph{arrive} and \emph{departure}. Firstly, \tem{} has an empty event list. But \teg{} had generated \emph{N}(can be changed) arrival events. Secondly the event manager get the events from \teg{} by first one or a range of time and insert to its list. And then \tem{} began processing events in its list. The processing details can be seen from Algorithm~\ref{alg:evenprocess}.

\NumberProgramstrue
\begin{algorithm}
\bfvariables
\begin{program}
eventList \gets{} []
|initialize|(statuses)
\DO
  \IF |size|(eventList)>0
  \THEN
    event \gets{} |pop|(eventList)
    packet \gets{} event[|packet|]
    \IF |typeof|(event)
    \THEN
      =|arrival| \to{}
      \rrcomment{Check the packet is arriving at the end, lost or inserted}
      \BEGIN
        \IF |arrivalPacket|(statuses, packet)
        \THEN
          =|finish| \to{}
          \rrcomment{The packet has gone to the end}
          \BEGIN
            |recordFinish|(statuses, packet)
          \END
          =|ok| \to{}
          \rrcomment{The packet can be processed}
          \BEGIN
            \rrcomment{Add the waiting time and completed time to packet }
            \rrcomment{timestamps, and move the packet source id}
            \rrcomment{to next one}
            packet \gets{} |refresh|(statuses,packet)
            waitTime, completedTime \gets{} |lastOfTimestamps|(packet)
            newEvents \gets{} |getEvents|(\teg{},
              waitTime,
              completedTime)
            |append|(eventList, newEvents)
            newEvent \gets{} |createDepartureEvent|(packet)
            |append|(eventList, newEvent)
          \END
          =|lost| \to{}
          \rrcomment{The packet should be lost because of no more buffer}
          \BEGIN
            |recordLost|(statuses, packet)
          \END
        \FI
      \END
      =|departure| \to{}
      \rrcomment{Release the buffer, get new arrival event from \teg{}}
      \BEGIN
        packet \gets{} |releaseBuffer|(statuses, packet)
        newEvent \gets{} |createArrivalEvent|(packet)
        |append|(eventList, newEvent)
      \END
    \FI
  \ELSE
    \rrcomment{The event list is empty, get the top of the \teg{}}
    newEvent \gets{} |pop|(\teg{})
    |append|(eventList, newEvent)
  \FI
\OD
\end{program}
\label{alg:evenprocess}
\end{algorithm}

For example, at first the \eventList{} is empty, so \tem{} get first one event from \teg{}. Now we suppose \eventList{$1_a$}, \tem{} should begin process the event. \tem{} get the first event, and check whether it can be inserted or not. If it can be inserted, it should insert all events which are in the range of beginning processing time to end processing time($[b,e)$), so \eventList{$2_a, 3_a, 4_a, \cdots{} , 1_d$}. \emph{The important thing is it is similar with inserting departure events to the hole event lists, but it only insert a range of events to the list.}
